package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.CourseEntity;
import com.tfjybj.itoo.teach.model.AcademyModel;
import com.tfjybj.itoo.teach.model.CourseModel;
import com.tfjybj.itoo.teach.provider.service.AcademyService;
import com.tfjybj.itoo.teach.provider.service.CourseService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * CourseController
 * course表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"course表接口"})
@RequestMapping(value = "/course")
@RestController
public class CourseController {

    @Resource
    private CourseService courseService;
    @Resource
    private AcademyService academyService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model CourseModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     * 张家豪-2018年11月9日20:31:50
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody CourseModel model) {
        if (StringUtils.isEmpty(model.getCode()) || model.getCode() == null || StringUtils.isEmpty(model.getName()) || model.getName() == null || StringUtils.isEmpty(model.getCourseCategory()) || model.getCourseCategory() == null || StringUtils.isEmpty(model.getCourseType()) || model.getCourseType() == null || StringUtils.isEmpty(model.getAcademyId()) || model.getAcademyId() == null) {
            return ItooResult.build(ItooResult.FAIL, "课程信息不能为空" + model);
        }
        CourseEntity courseEntity = new CourseEntity();
        BeanUtils.copyProperties(model, courseEntity);
        courseService.save(courseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        courseService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        courseService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model CourseModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改course")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody CourseModel model) {
        if (StringUtils.isEmpty(model.getCode()) || model.getCode() == null || StringUtils.isEmpty(model.getName()) || model.getName() == null || StringUtils.isEmpty(model.getCourseCategory()) || model.getCourseCategory() == null || StringUtils.isEmpty(model.getCourseType()) || model.getCourseType() == null || StringUtils.isEmpty(model.getAcademyId()) || model.getAcademyId() == null) {
            return ItooResult.build(ItooResult.FAIL, "课程信息不能为空" + model);
        }
        CourseEntity courseEntity = new CourseEntity();
        BeanUtils.copyProperties(model, courseEntity);
        courseService.updateById(courseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Course
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        CourseEntity courseEntity = courseService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", courseEntity);
    }

    /**
     * 分页查询所有Course
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 张家豪
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有Course")
    @GetMapping(value = {"/queryAllCourseInfo/{pageNo}/{pageSize}"})
    public ItooResult queryAllCourseInfo(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                         @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<CourseModel> courses = courseService.queryAllCourseInfo(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", courses);
    }

    /**
     * 根据课程名称模糊查询course
     *
     * @param name     课程名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的course
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据课程名称模糊查询course", notes = "分页根据课程名称模糊查询course")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryCourseByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryCourseByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                            @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<CourseModel> courseList = courseService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", courseList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据三个字段模糊查询分页显示
     *
     * @param code 课程代码，name 课程名称， academy 学院名称
     * @return courseModel
     * @author 张家豪
     * @since 2018.11.17 20:19
     */
    @ApiOperation(value = "根据三个字段模糊查询分页显示")
    @GetMapping(value = "pageFuzzyQueryCourse/{pageNo}/{pageSize}")
    public ItooResult pageFuzzyQueryCourse(@RequestParam(required = false, defaultValue = " ") String code, @RequestParam(required = false, defaultValue = " ") String name, @RequestParam(required = false, defaultValue = " ") String academy, @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo, @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<CourseModel> courseList = courseService.pageFuzzyQueryCourse(code, name, academy, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", courseList);
    }

    /**
     * 导出-根据代码名称学院模糊查询courseModel
     *
     * @param code    课程代码
     * @param name    课程名称
     * @param academy 学院id
     * @return 课程信息model
     * @author 张家豪
     * @since 2018.11.16 19:25
     */
    @ApiOperation(value = "导出-根据代码名称学院模糊查询courseModel")
    @GetMapping(value = "exportFile/fuzzyQueryCourseExport")
    public ItooResult fuzzyQueryCourseExport(HttpServletResponse response, @RequestParam(required = false, defaultValue = " ") String code, @RequestParam(required = false, defaultValue = " ") String name, @RequestParam(required = false, defaultValue = " ") String academy) {

        return courseService.fuzzyQueryCourseExport(response, code, name, academy);

    }

    /**
     * 查询所有学院和机构信息
     *
     * @return academyModel
     * @author 张家豪
     * @since 2018.11.19 14:58
     */
    @ApiOperation(value = "查询所有学院或者机构名称")
    @GetMapping(value = "/queryAllAcademy")
    public ItooResult queryProfessionOrInstitutionInfo() {
        List<AcademyModel> academyList = academyService.queryAcadOrInstInfo();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyList);
    }

    /**
     * 根据课程id集合查询课程信息
     *
     * @return academyModel
     * @author 张家豪
     * @since 2018.11.19 14:58
     */
    @ApiOperation(value = "根据课程id集合查询课程信息")
    @GetMapping(value = "/queryCourseByIds/{Ids}")
    public ItooResult queryCourseByIds(@ApiParam(name = "Ids", value = "课程ID集合", required = true) @PathVariable List<String> Ids) {
        List<CourseModel> courseInfo = courseService.queryCourseByIds(Ids);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseInfo);
    }

    /**
     * 根据课程类型和所属学院查询课程信息（类型必填，学院选填）
     *
     * @param academyId  学院id
     * @param courseType 课程类型
     * @return courseModel
     * @author 张家豪
     * @since 2018.11.22 14:51
     */
    @ApiOperation(value = "根据课程类型和所属学院查询课程信息（类型必填，学院选填）", notes = "根据课程类型和所属学院查询课程信息（类型必填，学院选填）")
    @GetMapping(value = {"/queryCourseByTypeAndAcademy"})
    public ItooResult queryCourseByTypeAndAcademy(@RequestParam(required = true) List<String> courseType, @RequestParam(required = false) String academyId) {
        if (academyId == null) {
            academyId = "";
        }
        List<CourseModel> courseInfo = courseService.queryCourseByTypeAndAcademy(courseType, academyId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseInfo);
    }

    /**
     * 批量导入course
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 张家豪
     * @since 1.0.0 2018年11月25日10:21:29
     */
    @ApiOperation(value = "批量导入course")
    @PostMapping(value = {"/importCourse"})
    public ItooResult importCourse(@RequestParam(value = "file") MultipartFile multipartFile,
                                   HttpServletRequest request, HttpServletResponse response) {
        return courseService.importCourse(multipartFile, request, response);
    }

    /**
     * 下载课程模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 张家豪
     * @since 2.0.0 2018年11月25日14:57:40
     */
    @ApiOperation(value = "下载课程模板，批量导入使用", notes = "下载课程模板，批量导入使用")
    @GetMapping(value = {"/downLoadTemplate"})
    public ItooResult downTemplate(HttpServletResponse response) {
        if (courseService.downTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载培养计划模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载培养计划模板失败");
        }
    }

    /**
     * 根据课程名称查询课程信息
     *
     * @param name 课程名称
     * @return courseEntity
     * @author 张家豪
     * @since 2018.11.28 14:38
     */
    @ApiOperation(value = "根据课程名称查询课程信息", notes = "根据课程名称查询课程信息")
    @GetMapping(value = {"/findCourseByName"})
    public ItooResult findCourseByName(@RequestParam(required = false) String name) {
        List<CourseEntity> courseInfo = courseService.findCourseByName(name);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseInfo);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return 课程信息model
     * @author 张家豪
     * @since 2018.11.16 19:25
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (courseService.downErrorList(errorListId,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }

    }

    /**
     * 根据code查询课程信息
     *
     * @param code 课程代码
     * @author 张家豪
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据code查询课程信息")
    @GetMapping(value = {"/isExistByCode/{code}"})
    public ItooResult isExistByCode(@PathVariable String code) {
       List<CourseModel> courses = courseService.isExistByCode(code);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", courses);
    }

    /**
     * 根据name查询课程信息
     *
     * @param name 课程代码
     * @author 张家豪
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据name查询课程信息")
    @GetMapping(value = {"/isExistByName/{name}"})
    public ItooResult isExistByName(@PathVariable String name) {
        List<CourseModel> courses = courseService.isExistByName(name);
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", courses);
    }

    /**
     * 根据学院ID查询所有课程
     *
     * @param academyId 学院id
     * @return courseModel
     * @author 宋喆
     * @since 2018.12.14 19:48
     */
    @ApiOperation(value = "根据学院id查询课程信息")
    @GetMapping(value = "/queryCourseByAcademyId/{academyId}")
    public ItooResult queryCourseByAcademyId(@ApiParam(value = "学院id", required = true) @PathVariable String academyId) {
        List<CourseModel> courseModelLists = courseService.queryCourseByacademyId(academyId);
        return ItooResult.build(ItooResult.SUCCESS,"查询完成",courseModelLists);
    }

    @ApiOperation(value = "根据课程id查询课程名称")
    @GetMapping(value = "/queryCourseById")
    public CourseEntity findCourse(String id){
        CourseEntity courseEntity = courseService.findCourse(id);
        return courseEntity;
    }

}
